package com.leetcode.algorithm;

/**
 * @ProjectName: leetcode
 * @Package: com.leetcode.algorithm
 * @description: 单链表反转
 * @Author: liyc
 * @CreateDate: 2020/1/2 4:48 PM
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2020</p>
 */
public class ListRevert {
    public static class Node {
        int val;
        Node next;

        public Node(int x) {
            val = x;
        }
    }

    /**
     * @param
     * @return
     * @description
     * @author liyc
     * @date 2020/1/2
     */
    public static Node reverseByLoop(Node head) {

        Node pre = null;
        Node next = null;

        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
//        Node p = pre;
//        while (p != null) {
//            System.out.println(p.val);
//            p = p.next;
//        }

        return pre;
    }


    public static void main(String[] args) {
        Node node = new Node(1);
        node.next = new Node(2);
        node.next.next = new Node(3);
        node = reverseByLoop(node);
    }
}


